﻿using System;
using fastCSharp;
using System.Collections.Generic;

namespace diantou.web.exam
{
    /// <summary>
    /// 用户考试
    /// </summary>
    [fastCSharp.code.cSharp.webView.clientType(Name = "diantou.userExam")]
    struct userExam
    {
        /// <summary>
        /// 企业考试标识
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public int Id
        {
            get { return Exam == null ? 0 : Exam.Id; }
        }
        /// <summary>
        /// 企业考试
        /// </summary>
        public diantou.examDataAccess.exam Exam;
        /// <summary>
        /// 用户信息
        /// </summary>
        public diantou.examDataAccess.examUser User;
        /// <summary>
        /// 是否结束考试
        /// </summary>
        public bool IsEnd
        {
            get
            {
                return Exam.IsEnd || User.IsFinally;
            }
        }
        /// <summary>
        /// 考试回答问题
        /// </summary>
        private dataModel.examAnswerBase[] answers;
        /// <summary>
        /// 考试回答问题
        /// </summary>
        private dataModel.examAnswerBase[] Answers
        {
            get
            {
                if (answers == null)
                {
                    answers = problem.AnswerCacheClient.get(User.Id).Value;
                    if (answers == null) answers = nullValue<dataModel.examAnswerBase>.Array;
                }
                return answers;
            }
        }
        /// <summary>
        /// 回答过的问题的题目的分数
        /// </summary>
        private int? answerPoint;
        /// <summary>
        /// 回答过的问题的题目的分数
        /// </summary>
        public int AnswerPoint
        {
            get
            {
                if (answerPoint == null)
                {
                    if (examDataAccess.clientCache.ExamQuestion.IsData && examDataAccess.clientCache.ExamProblem.IsData)
                    {
                        int point = 0;
                        foreach (dataModel.examAnswerBase answer in Answers)
                        {
                            switch (answer.Type)
                            {
                                case dataModel.examQuestionBase.type.Radio:
                                    point += dataModel.examQuestion.RadioPoint;
                                    break;
                                case dataModel.examQuestionBase.type.MultipleChoice:
                                    point += dataModel.examQuestion.MultipleChoicePoint;
                                    break;
                                case dataModel.examQuestionBase.type.Question:
                                    point += examDataAccess.clientCache.ExamQuestion[answer.QuestionId].Point;
                                    break;
                                case dataModel.examQuestionBase.type.Problem:
                                    point += examDataAccess.clientCache.ExamProblem[answer.QuestionId].BasePoint;
                                    break;
                            }
                        }
                        answerPoint = point;
                    }
                    else answerPoint = diantou.examDataAccess.tcpCall.examAnswer.getPoint(User.Id);
                }
                return (int)answerPoint;
            }
        }
        /// <summary>
        /// 竞赛相关收藏客户端缓存
        /// </summary>
        private static readonly fastCSharp.sql.logCache<int, diantou.examDataAccess.examAnswer.question[]>.array questionCache = new fastCSharp.sql.logCache<int, diantou.examDataAccess.examAnswer.question[]>.array(diantou.examDataAccess.tcpCall.examItem.onExamLog, 4);
        /// <summary>
        /// 考试回答问题
        /// </summary>
        private static readonly Dictionary<dataModel.examQuestion.questionType, diantou.dataModel.examAnswerBase> nullAnswers = new Dictionary<dataModel.examQuestion.questionType, dataModel.examAnswerBase>();
        /// <summary>
        /// 用户题目信息列表
        /// </summary>
        public userQuestionAnswer[] UserQuestions
        {
            get
            {
                if (Exam.IsStart)
                {
                    fastCSharp.sql.logCache<int, diantou.examDataAccess.examAnswer.question[]>.array.value questions = questionCache.Get(User.ExamId);
                    if (questions.Value == null)
                    {
                        questions.Value = diantou.examDataAccess.tcpCall.examItem.getQuestion(User.ExamId);
                        questionCache.Set(ref questions);
                    }
                    Dictionary<dataModel.examQuestion.questionType, diantou.dataModel.examAnswerBase> answers = Answers.Length == 0 ? nullAnswers : Answers.getDictionary(answer => new dataModel.examQuestion.questionType { Type = answer.Type, QuestionId = answer.QuestionId });
                    userQuestionAnswer[] values = new userQuestionAnswer[questions.Value.Length];
                    for (int index = values.Length; index != 0; )
                    {
                        --index;
                        values[index].Set(ref questions.Value[index], answers, User, index + 1);
                    }
                    return values;
                }
                return nullValue<userQuestionAnswer>.Array;
            }
        }
    }
}
